package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;


import entity.Message;
import service.MessageService;
import service.impl.MessageServiceImpl;

public class MessageListServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		List<Message> messageList = new ArrayList<Message>(); 
		MessageService ms = new MessageServiceImpl();
		String uname = (String) request.getSession().getAttribute("uname");
		messageList = ms.getMessageByReceiveUname(uname);
		request.setAttribute("list", messageList);
		
		String jsonStr = JSON.toJSONString(messageList); //fastjson把list转json
		//System.out.println("json"+jsonStr);
		//request.getRequestDispatcher("index.jsp").forward(request, response); 
		
		//writer输出返回json
		PrintWriter writer = response.getWriter();
		writer.write(jsonStr);
		writer.close();

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
